package com.guigu.www.tangyingmeng.controller;

import com.guigu.www.pojo.BespeakPoject;
import com.guigu.www.pojo.BespeakSetMeal;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.tangyingmeng.service.TymBespeakSetMealService;
import com.guigu.www.utils.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class TymBespeakSetMealController {

    @Autowired
    private TymBespeakSetMealService service;

    @PostMapping("/selsetmealall")
    public PageVo<BespeakSetMeal> selsetmealall(@RequestBody Map<String, String> map){
        // System.out.println(map);
        PageVo<BespeakSetMeal> pageVo = service.selsetmealall(map);
        // System.out.println(pageVo);
        return pageVo;
    }

    //查询可用项目的方法
    @PostMapping("/queryPojects")
    public List<BespeakPoject> queryPojects(){
        return service.queryPojects();
    }

    // 添加套餐的方法
    @PostMapping("/submitSetMeal")
    public Integer submitSetMeal(MultipartFile smImage, BespeakSetMeal setmeal,Integer[] arr) throws IOException {
        // 获取上传的文件流
        InputStream inputStream = smImage.getInputStream();
        String originalFilename = smImage.getOriginalFilename();
        //获取源文件前缀
        String fileNamePrefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
        //获取源文件后缀
        String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 将源文件前缀之后加上时间戳避免重名
        String newFileNamePrefix = fileNamePrefix + new Date().getTime();
        //得到上传后新文件的文件名
        String newFileName = newFileNamePrefix+fileNameSuffix;
        boolean b = FtpUtil.uploadFile(newFileName, inputStream);
        setmeal.setBesSmImage(newFileName);
        service.submitSetMeal(setmeal,arr);
        return 0;
    }

    // 修改套餐的方法
    @PostMapping("/submitUp")
    public Integer submitUp(MultipartFile besSmImageUp, BespeakSetMeal setmeal,Integer[] arr) throws IOException {
        if(besSmImageUp==null){
            service.submitUp(setmeal,arr);
        } else {
            // 获取上传的文件流
            InputStream inputStream = besSmImageUp.getInputStream();
            String originalFilename = besSmImageUp.getOriginalFilename();
            //获取源文件前缀
            String fileNamePrefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            //获取源文件后缀
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 将源文件前缀之后加上时间戳避免重名
            String newFileNamePrefix = fileNamePrefix + new Date().getTime();
            //得到上传后新文件的文件名
            String newFileName = newFileNamePrefix+fileNameSuffix;
            boolean b = FtpUtil.uploadFile(newFileName, inputStream);
            setmeal.setBesSmImage(newFileName);
            service.submitUp(setmeal,arr);
        }
        return 0;
    }

    // 查看套餐详情的方法
    @PostMapping("/clickUp")
    public BespeakSetMeal clickUp(Integer besSmId){
        return service.clickUp(besSmId);
    }

    // 生成模板的方法
    @PostMapping("/createPDF")
    public Integer createPDF(String fileName,Integer besSmId){
        // System.out.println(fileName);
        // System.out.println(besSmId);
        service.createPDF(fileName,besSmId);
        return 0;
    }
}
